package com.tb.ClassSchedule;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.transition.Visibility;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashSet;
import java.util.Iterator;

/**
 * Created by guich on 2016/8/17.
 */
public class MainWeekView extends FrameLayout {

    //定义课表的大小
    public static int CELL_WIDTH;
    public static int CELL_HEIGHT;
    //指示器大小
    public static int SECTION_WIDTH;
    public static int SECTION_HEIGHT;

    //一天课程的布局数组
    private LinearLayout[] day;

    private LinearLayout section;
    private View guider;

    public MainWeekView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.one_week_layout,this);
        section = (LinearLayout) findViewById(R.id.section);
        LinearLayout monday  = (LinearLayout) findViewById(R.id.mon);
        LinearLayout tuesday = (LinearLayout) findViewById(R.id.tues);
        LinearLayout wednesday = (LinearLayout) findViewById(R.id.wed);
        LinearLayout thursday = (LinearLayout) findViewById(R.id.thur);
        LinearLayout friday = (LinearLayout) findViewById(R.id.fri);
        LinearLayout saturday = (LinearLayout) findViewById(R.id.sat);
        LinearLayout sunday = (LinearLayout) findViewById(R.id.sun);
        day = new LinearLayout[]{monday, tuesday, wednesday, thursday, friday, saturday, sunday};
        guider = findViewById(R.id.guider);

        DisplayMetrics metrics           = getResources().getDisplayMetrics();
        CELL_WIDTH    = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,67,metrics);
        CELL_HEIGHT   = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,metrics);
        //指示器大小
        SECTION_WIDTH = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,25,metrics);
        SECTION_HEIGHT = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,20,metrics);



    }

    public void drawClasses(HashSet<Lesson> lessonsInfos, int selectedWeek){

        if(lessonsInfos.size() == 0) {
            guider.setVisibility(VISIBLE);
        }else{
            guider.setVisibility(GONE);

            //绘制节次
            section.removeAllViews();
            section.addView(new TextView(getContext()),SECTION_WIDTH, SECTION_HEIGHT);
            for(int i = 0; i < LessonSet.sectionCount ; i++){
                TextView tv = new TextView(getContext());
                tv.setText(" " + (i + 1));
                section.addView(tv,SECTION_WIDTH, CELL_HEIGHT);
            }
            View view;

            Lesson lesson  = new Lesson();
            Lesson lesson2 = new Lesson();              //记录上课时间未到的课程

            //绘制课程
            for (int i = 0; i < LessonSet.dayCount; i++) {
                day[i].removeAllViews();
                TextView tv = new TextView(getContext());
                tv.setGravity(Gravity.CENTER);
                tv.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorLabel));
                String str = "";
                switch (i) {
                    case 0:
                        str = "周一";
                        break;
                    case 1:
                        str = "周二";
                        break;
                    case 2:
                        str = "周三";
                        break;
                    case 3:
                        str = "周四";
                        break;
                    case 4:
                        str = "周五";
                        break;
                    case 5:
                        str = "周六";
                        break;
                    case 6:
                        str = "周日";
                        break;
                }
                tv.setText(str);
                day[i].addView(tv, CELL_WIDTH, SECTION_HEIGHT);

                for (int j = 0; j < LessonSet.sectionCount; j++) {


                    boolean notTime = false;                            //记录是否存在上课时间未到的课程
                    boolean interrupted = false;                        //人工中断标记
                    for (Lesson lessonsInfo : lessonsInfos) {
                        lesson = lessonsInfo;
                        if ((lesson.getStartSection() == j + 1) && (lesson.getWeekday() == i + 1)) {
                            if ((lesson.getStartWeek() <= selectedWeek) && (lesson.getEndWeek() >= selectedWeek)) {
                                notTime = false;
                                interrupted = true;
                                break;
                            } else {
                                lesson2 = lesson;
                                notTime = true;
                            }

                        }

                    }

                    if(!interrupted){
                        //说明没匹配到完全匹配的课程，再看是否存在时间未到的课程
                        if (notTime) {
                            lesson = lesson2;
                        } else {
                            lesson = new Lesson();
                        }
                    }

                    int n = 1 + lesson.getEndSection() - lesson.getStartSection();
                    view = new LessonViewForWeek(getContext(), lesson, notTime);
                    if (notTime) {
                        view.setAlpha(0.3f);
                    }
                    day[i].addView(view, CELL_WIDTH, n * CELL_HEIGHT);
                    j = j + n - 1;

                }
            }

        }


    }




}
